//
//  YLAccountSettingController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/1.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLAccountSettingController.h"
#import "YLGlobeHeader.h"
#import <Masonry.h>
#import "YLAccountSettingViewModel.h"
#import "YLTitleArrowNoSubCell.h"
#import "YLTitleAllowCell.h"
#import "YLLoginOutView.h"
#import "WHNavBackButton.h"
#import "SCSecureHelper.h"
#import "YLAccountTool.h"
#import "YLSwitchRootController.h"
#import "YLAccountTool.h"
@interface YLAccountSettingController ()
@property (nonatomic, strong) YLAccountSettingViewModel *viewModel;
@end

@implementation YLAccountSettingController

- (YLAccountSettingViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLAccountSettingViewModel alloc] init];
    }
    return _viewModel;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    BOOL gestureOpen = [SCSecureHelper gestureOpenStatus];
    YLIconTitleModelGroup *group = self.viewModel.groups[1];
    YLIconTitleContentModel *model = group.models.firstObject;
    if (gestureOpen) {
        model.content = @"已开启";
    } else {
        model.content = @"去设置";
    }
    [self.tableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账号设置";
 


    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleArrowNoSubCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleArrowNoSubCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleAllowCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleAllowCell cellID]];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.viewModel.kefuSubject subscribeNext:^(id  _Nullable x) {
        
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", x];
        if (@available(iOS 11.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        } else {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
            });
        }

    }];
}





- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YLIconTitleModelGroup *group = self.viewModel.groups[section];
    return group.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLIconTitleModelGroup *group = self.viewModel.groups[indexPath.section];
    YLIconTitleModel *model = group.models[indexPath.row];
    
    if ([model isKindOfClass:[YLIconTitleContentModel class]]) {
        YLTitleAllowCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleAllowCell cellID]];
        cell.contentModel = (YLIconTitleContentModel *)model;
        return cell;
    }
    
    YLTitleArrowNoSubCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleArrowNoSubCell cellID]];
    
    cell.hiddenLine = YES;
  
    
    cell.iconTitleModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section == self.viewModel.groups.count - 1) {
        return 90;
    }
    return 10;
}

// tableView 如果是Gruop类型的话，section之间的间距变宽，执行返回高度的同时还需要执行return UIView的代理
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    __weak typeof(self) weakSelf = self;
    if (section == self.viewModel.groups.count - 1) {
        YLLoginOutView *outView = [[YLLoginOutView alloc] init];
        outView.loginOutBlock = ^{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"退出确认" message:@"是否退出登录" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *loginOutAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [SCSecureHelper openGesture:NO];
                [YLAccountTool noAccount];
                [YLSwitchRootController switchRootController];
            }];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:loginOutAction];
            [alertController addAction:cancelAction];
            [weakSelf presentViewController:alertController animated:YES completion:nil];

        };
        return outView;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [YLTitleArrowNoSubCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        YLIconTitleModelGroup *group = self.viewModel.groups[indexPath.section];
        YLIconTitleModel *model = group.models[indexPath.row];
        if (model.clickBlock) {
            NSString *name = model.clickBlock();
            if (name.length > 0) {
                [self.navigationController pushViewController:[[NSClassFromString(name) alloc] init] animated:YES];
            }
        }
}



    
@end
